from django.newforms import forms, fields, widgets
from django.newforms.fields import *
from django.newforms.models import ModelChoiceField
from django.utils.datastructures import SortedDict

__all__ = fields.__all__ + ('ModelChoiceField',)

def get_admin_form(module_type=None):
    attrs = SortedDict(save=save_admin_form)

def save_admin_form(form):
    for field in form.fields:
        print field

def form_for_module(module):
    """
    Return a customized newforms Form for the specified module.
    """
    name = module.__class__.__name__ + 'Form'
    attrs = {'__module__': module.__class__.__module__}

    for name, field in module._fields.items():
        if module.id:
            field.initial = getattr(module, name)
        attrs[name] = field

    return type(name, (forms.Form,), attrs)

def form_for_modules(modules):
    """
    Return a customized newforms Form for all the specified modules.
    """
    from django.newforms.forms import pretty_name

    attrs = {'__module__': __name__}

    for module in modules:
        for name, field in module._fields.items():
            if module.id:
                field.initial = getattr(module, name)
            attrs['module__%s__%s' % (module.id, name)] = field

    return type('ModuleEditor', (forms.Form,), attrs)
